import { _decorator, Component, Label, Node, ProgressBar } from 'cc';
import { GameMgr } from '../manager/GameMgr';
import { StorageUtil } from '../util/StorageUtil';
import { EventMgr } from '../manager/EventMgr';
import { EventType } from '../enum/EventType';
const { ccclass, property } = _decorator;

@ccclass('UIItem/PassRewardItem')
export class PassRewardItem extends Component {

    @property(ProgressBar)
    private progressBar: ProgressBar = null

    @property(Label)
    private lbProgress: Label = null

    protected onLoad(): void {
        EventMgr.on(EventType.GetPassReward, this.onUpdateState, this)
    }

    protected onDestroy(): void {
        EventMgr.off(EventType.GetPassReward, this.onUpdateState, this)
    }

    protected start(): void {
        this.onUpdateState()
    }

    private onUpdateState(): void {
        const passRewardLevel: number = StorageUtil.getItem('passRewardLevel', 0)
        const passLevel: number = GameMgr.CurPassedLevel
        let targetPassLevel: number = Math.ceil(passLevel / 5) * 5

        if (passRewardLevel === targetPassLevel) targetPassLevel += 5

        const progress: number = passLevel / targetPassLevel
        this.progressBar.progress = progress
        this.lbProgress.string = `${passLevel}/${targetPassLevel}`

    }

}


